package com.mohan.service.impl;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mohan.bean.WxUserInfo;
import com.mohan.common.HttpsReq;
import com.mohan.dao.UserDao;
import com.mohan.json.WxLogin;
import com.mohan.pojo.User;
import com.mohan.rep.LoginRep;
import com.mohan.service.LoginService;
import com.mohan.tool.Base64CC;
import com.mohan.tool.Token;
import com.mohan.tool.UserIdBuild;
import com.mohan.tool.UserMap;
import com.mohan.util.WxLoginUtil;

import com.mohan.util.TokenUserInfo;

@Service("loginService")
public class LoginServiceImpl implements LoginService {
	@Resource
	private UserDao userDao;

	@SuppressWarnings("finally")
	@Override
	public LoginRep getOpenId(String code){
		/**
		 * 1.根据code从微信服务器获取openid和session_key,并解析到WxLoginUtil
		 */
		LoginRep lRep = null;
		String wxOpenIdRep = HttpsReq.httpsGet(code);
		WxLoginUtil wxLoginUtil = new WxLoginUtil();
		boolean openIdParser = WxLogin.paserOpenId(wxOpenIdRep, wxLoginUtil);
		if (openIdParser) {
			User user = this.userDao.selectUserByOpenId(wxLoginUtil.getOpenid());
			if (user == null) {
				/**
				 * 新用户
				 * 添加到数据库
				 */
				lRep = new LoginRep();
				user = new User();
				user.setOpenId(wxLoginUtil.getOpenid());
				user.setUnionId(wxLoginUtil.getUnionid());
				user.setUserType(1);//微信用户
				user.setUserStatus(1);//用户状态-正常使用
				user.setRole(3);//用户角色-普通用户
				dealNewUser(user.getOpenId(),user);
				lRep.setIsNew(1);
				
				try {
					lRep.setToken(Base64CC.encode(UserMap.UserId2TokenMap.get(user.getUserId()).getToken()));
					lRep.setUserId(Base64CC.encode(user.getUserId()));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					return lRep;
				}
			}else {
				/**
				 * 老用户
				 */
				/**
				 * 生成token
				 */
				try {
					lRep = new LoginRep();
					String token = Token.createToken();
					TokenUserInfo ttUserInfo = new TokenUserInfo();
					ttUserInfo.setRole(user.getRole());
					ttUserInfo.setToken(token);
					ttUserInfo.setUserId(user.getUserId());
					ttUserInfo.setUserStatus(user.getUserStatus());
					ttUserInfo.setUserType(user.getUserType());
					UserMap.UserId2TokenMap.put(user.getUserId(), ttUserInfo);
					lRep.setIsNew(2);
					lRep.setToken(Base64CC.encode(token));
					lRep.setUserId(Base64CC.encode(user.getUserId()));
					WxUserInfo wInfo = new WxUserInfo();
					wInfo.setAvatarUrl(Base64CC.encode(user.getAvatarUrl()));
					wInfo.setCity(Base64CC.encode(user.getCity()));
					wInfo.setCountry(Base64CC.encode(user.getCountry()));
					wInfo.setGender(Base64CC.encode(user.getGender()));
					wInfo.setLanguage(Base64CC.encode(user.getLanguage()));
					wInfo.setNickName(Base64CC.encode(user.getNickName()));
					wInfo.setPhoneNum(Base64CC.encode(user.getPhoneNum()));
					wInfo.setProvince(Base64CC.encode(user.getProvince()));
					wInfo.setUserId(Base64CC.encode(user.getUserId()));
					wInfo.setUserName(Base64CC.encode(user.getUserName()));
					wInfo.setUserStatus(user.getUserStatus());
					lRep.setUser(wInfo);
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					return lRep;
				}
			}
		}
		
		return lRep;
	}
	
	/**
	 * 新用户处理
	 * 
	 * @param openid
	 * @param user
	 */
	private void dealNewUser(String openid, User user) {
		/**
		 * 生成token
		 */
		String token = Token.createToken();

		/**
		 * 生成userid
		 */
		String userId = UserIdBuild.buildUserId();		
		user.setUserId(userId);		
		/**
		 * 把user加入数据库
		 */
		userDao.insertUser(user);
		/**
		 * token存起来
		 */
		TokenUserInfo ttUserInfo = new TokenUserInfo();
		ttUserInfo.setRole(user.getRole());
		ttUserInfo.setToken(token);
		ttUserInfo.setUserId(userId);
		ttUserInfo.setUserStatus(user.getUserStatus());
		ttUserInfo.setUserType(user.getUserType());
		UserMap.UserId2TokenMap.put(userId, ttUserInfo);		
	}

}
